package com.example.houtai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.houtai.entity.SysDept;
import com.example.houtai.entity.SysRole;
import com.example.houtai.entity.SysUser;
import com.example.houtai.entity.TbBusiness;
import com.example.houtai.service.ISysDeptService;
import com.example.houtai.service.ISysRoleService;
import com.example.houtai.service.ISysUserService;
import com.example.houtai.service.ITbBusinessService;
import com.org.dto.BusinessDto;
import com.org.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 商户表 前端控制器
 * </p>
 *
 * @author 龚梦
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/sys/business")
public class TbBusinessController {

    @Autowired
    private ITbBusinessService businessService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping("list")
    public Page<TbBusiness> list(@RequestBody BusinessDto businessDto){

        Page<TbBusiness> page = new Page<>();

        page.setCurrent(businessDto.getPageNum());

        page.setSize(businessDto.getPageSize());

        return businessService.list(page);
    }

    @RequestMapping("pass")
    public ResultEntity pass(@RequestBody TbBusiness business){

        List<SysDept> sysDepts = sysDeptService.listByIds(Arrays.asList(business.getId()));

        if(CollectionUtils.isEmpty(sysDepts)){
            return ResultEntity.error("401","请在部门下添加商户",true);
        }

        List<SysRole> sysRoles = sysRoleService.listByIds(Arrays.asList(sysDepts.get(0).getDeptId()));

        if(CollectionUtils.isEmpty(sysRoles)){
            return ResultEntity.error("401","请在角色下添加商户",true);
        }

        List<SysUser> userList = sysUserService.listByIds(Arrays.asList(sysDepts.get(0).getDeptId()));

        if (CollectionUtils.isEmpty(userList)) {
            return ResultEntity.error("401","请在用户下添加商户",true);
        }else {
            List<Long> roleLong = new ArrayList<>();
            for (int i = 0; i < sysRoles.size(); i++) {
                SysRole sysRole = sysRoles.get(i);
                roleLong.add(sysRole.getRoleId());
            }
            SysUser user = userList.get(0);
            user.setApprovalFlag(1);
            sysUserService.updateById(user);
        }

        try {
            business.setStatus(1);
            businessService.updateById(business);
            return ResultEntity.ok("200","操作成功",false);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("修改失败");
        }
    }

    @RequestMapping("nopass")
    public ResultEntity nopass(@RequestBody TbBusiness business){
        try {
            business.setStatus(2);
            businessService.updateById(business);
            return ResultEntity.ok("200","操作成功",false);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("修改失败");
        }
    }

    @RequestMapping("add")
    public ResultEntity add(@RequestBody TbBusiness business){
        business.setCreateTime(new Date());
        business.setStatus(3);
        System.out.println(business+"-=-=-=-=-=-=");
        boolean b = businessService.save(business);
        return ResultEntity.ok(b);
    }

}
